import medicinalviewApi from "@/api/medicinalview";
import productbaseApi from "@/api/productbase";
import ViewMessage from "@/views/resourcemg/zymanage/productionArea/index.vue";
import VueElementLoading from 'vue-element-loading';
import medicinalProductBaseApi from "@/api/medicinalproductbase";
import EnvironmentForm from "./form/environmentForm";
import CompanyForm from "./form/companyForm";
import MdvalueForm from "./form/mdValueForm";

// 局域网测试
// const BASEURL = "http://192.168.1.109:8031";

const BASEURL = "https://image.zysuyuan.cn:8031/";

const components = {
    ViewMessage,
    VueElementLoading,
    EnvironmentForm,
    CompanyForm,
    MdvalueForm
};

const data = function () {
    return {
        path: localStorage.getItem(localStorage.getItem("index")),
        BASEURL,
        imgpath: "",
        name: "",
        onelist: [
            {
                mLatinName: "",
                mOriginName: "",
                mOriginLatinName: "",
                mOriginInterview: "",
                mRemarks: "",
                imgPath: "",
                mPriceLink: ""
            }
        ],
        list: [],
        imgs: [],
        radio: null,
        currentSelectItem: {},
        maplocation: {},
        searchMap: {
            id: "",
            page: "",
            size: "",
            bName: "",
            bArea: "",
            tYield: "",
            tMedicinalQuality: "",
            bInputLocation: "",
            bProvinceSeek: "", // 省
            bCitySeek: "", //市
            bRegionSeek: "", //区
            province: '',
            city: '',
            area: '',
        }, // 条件查询绑定的条件值
        dialogEnviroForm: false, //生态环境弹窗
        dialogbaseArea: false, //基地照片弹窗
        dialogCompanyForm: false, //公司详情弹窗
        dialogFormGps: false, //gps弹窗
        dialogmedicineValue: false, //药材质量弹窗
        enviroform: {
            bWaterEnvironment: "",
            bSoilEnvironment: "",
            bAirEnvironment: "",
            bTotalityAppraise: ""
        },
        companyform: {
            companyName: "",
            companyPosition: "",
            companyPerson: "",
            companyContact: "",
            companyMedicinal: "",
            companyRemarks: "",
        },
        mdValueform: {
            tMedicinalSharp: "", // 性状
            tMedicinalSharpImgPath: "",// 性状图片
            tMedicinalIdentify: "",// 鉴别
            tMedicinalIdentifyImgPath: "",// 鉴别图片
            tMedicinalExtractum: "",// 浸出物
            tMedicinalContentsCheck: "",// 含量测定
            tMedicinalFeatureImgPath: "",// 特征图谱或指纹图谱
            tMedicinalFeatureImgInfo: "",// 图谱说明
            tMedicinalQuality: ""// 总评
        },
        // 百度地图
        center: { lng: 0, lat: 0 },
        // 图片展示类型
        fit: "contain",
        zoom: 3,
        // 调整装备
        locData: {
            ids: 0, //设置定位id
            longitude: "", //设置定位经度
            latitude: "" //设置定位纬度
        },
    };
};

const watch = {
    $route(to, from) {
        this.path = localStorage.getItem(localStorage.getItem("index"));
        this.fetchData();
        this.fetchProductBase();
    },
};

const methods = {
    // 跳转价格页面
    showprice(link) {
        window.open(link, '_blank');
        // 以下方法使用跳转到iframe的路由，会涉及跨域问题，导致控制台报错，看着很不爽，所以用跳转外链的方法
        // this.$router.push({
        //   name: "zyprice",
        //   params: { link: this.onelist[0].mPriceLink }
        // });
    },
    getTitle() {
        return this.$route.params.name;
    },
    fetchData() {
        var vm = this.$store;
        vm.commit('updateLoading', true);
        console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
        medicinalviewApi.zyMedicinalOneView(localStorage.getItem("id")).then(resp => {
            this.imgpath = this.$store.state.zypictures.imgpaths[
                this.$store.state.zymedicinalview.index
            ];
            console.log("图片地址" + this.imgpath);
            this.onelist = resp.data.rows;
            console.log("查询结果" + this.onelist);
            if (resp.data.rows) {
                vm.commit('updateLoading', false);
                console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
            }
        });
    },
    fetchProductBase() {
        console.log(this.searchMap);
        console.log("当前页数" + this.currentPage);
        this.searchMap.id = localStorage.getItem("id");
        this.searchMap.page = this.currentPage;
        this.searchMap.size = this.pageSize;
        var vm = this.$store;
        vm.commit('updateLoading', true);
        console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
        medicinalProductBaseApi.selectmBase(this.searchMap).then(resp => {
            this.list = resp.data.rows;
            console.log("查询成功:", resp.data);
            this.total = resp.data.total;
            if (resp.data.rows) {
                vm.commit('updateLoading', false);
                console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
            }
        });
    },
    handleSelectionChange(row) {
        console.log(row);
        this.currentSelectItem = row;
    },
    // 当每页显示条数改变后，被触发，val是最新的显示条数
    handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.fetchProductBase();
    },
    // 当页码改变后，被触发，val是最新的页码
    handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.fetchProductBase();
    },
    // 点击一行
    chooseone(row) {
        this.radio = row.id;
        this.maplocation.latitude = row.bLatitude;
        this.maplocation.longitude = row.bLongitude;
    },
    // 弹出生态环境
    openenvironment(row) {
        this.enviroform.bWaterEnvironment = row.bWaterEnvironment;
        this.enviroform.bSoilEnvironment = row.bSoilEnvironment;
        this.enviroform.bAirEnvironment = row.bAirEnvironment;
        this.enviroform.bTotalityAppraise = row.bTotalityAppraise;
        console.log(this.enviroform);
        this.dialogEnviroForm = true;
    },
    // 查看基地照片
    openbasephoto(row) {
        console.log(row);
        var vm = this.$store;
        vm.commit('updateImgLoading', true);
        console.log('是否开启加载', this.$store.state.elementloading.imgLoading)
        productbaseApi.selectImgsList(row.tProductBaseId).then(resp => {
            this.imgs = resp.data.rows;
            console.log(this.imgs);
            if (resp.data.rows) {
                vm.commit('updateImgLoading', false);
                console.log('是否开启加载', this.$store.state.elementloading.imgLoading)
            }
        });
        this.dialogbaseArea = true;
    },
    // 关闭查看基地图片t弹窗
    closeImgs() {
        this.imgs = [];
        this.dialogbaseArea = false;
    },
    // 关闭图片弹窗之前的回调,及点击弹窗右上角叉叉关闭按钮之前的回调方法
    // 为了将图片数组清空，弹窗滚轮置顶
    imgsDialogClose() {
        this.imgs = [];
        this.dialogbaseArea = false;
    },
    // 弹出公司详情
    openCompany(row) {
        this.companyform.companyName = row.bCompanyName;
        this.companyform.companyPosition = row.bCompanyPosition;
        this.companyform.companyPerson = row.bCompanyPerson;
        this.companyform.companyContact = row.bCompanyContact;
        this.companyform.companyMedicinal = row.bCompanyMedicinal;
        this.companyform.companyRemarks = row.bCompanyRemarks;
        this.dialogCompanyForm = true;
    },

    //弹出gps窗口
    showgps(row) {
        console.log(row);
        this.center.lng = row.bLongitude;
        this.center.lat = row.bLatitude;
        console.log("经度:" + this.center.lng);
        console.log("纬度:" + this.center.lat);
        this.zoom = 15;
        this.dialogFormGps = true;
        this.$nextTick(() => {
            this.zoom = 15;
        })
    },
    // 百度地图方法
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(val) {
        var i = 0,
            valLen = val.length;

        if (valLen == 0) {
            this.locData.ids = 0;
        } else {
            for (i; i < valLen; i++) {
                if (i == 0) {
                    this.locData.ids = val[i].id;
                } else if (i > 0) {
                    this.locData.ids += "," + val[i].id;
                }
            }
        }
    },
    // 设置定位弹出框
    setLoc(formName) {
        if (this.locData.ids == 0) {
            this.$message({
                message: "请选中装备后再设置定位",
                type: "warning"
            });
        } else {
            this.dialogFormGps = true;
            map.clearOverlays();
        }
    },
    // 百度地图
    mapReady({ BMap, map }) {
        // this.center.lng = this.maplocation.longitude;
        // this.center.lat = this.maplocation.latitude;
        this.zoom = 15;
        window.map = map; //将map变量存储在全局
    },
    // positionReady({map}) {
    //     this.position.lng = 116.397128;
    //     this.position.lat = 39.916527;
    //     this.zoom = 15;
    //     window.map = map; //将map变量存储在全局
    // },
    // 地图点击事件
    getClickInfo(e) {
        map.clearOverlays();
        var myMarker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
        map.addOverlay(myMarker);
        this.zoom = e.target.getZoom()
        this.locData.longitude = e.point.lng;
        this.locData.latitude = e.point.lat;
        this.center.lng = e.point.lng;
        this.center.lat = e.point.lat;
        // this.fpojo.bLongitude = e.point.lng;
        // this.fpojo.bLatitude = e.point.lat;
    },
    // 设置定位确认
    locSubmit(formName) {
        if (this.locData.longitude == "") {
            addOper.$message({
                message: "请单击地图设置定位后再提交",
                type: "warning"
            });
        } else {
            //  向后台提交locData数据
        }
        this.dialogFormGps = false;
    },
    // 设置定位取消
    locCancle(formName) {
        map.clearOverlays();
        this.dialogFormGps = false;
    },

    // 跳转到原料去向
    toMedicinalGone(row) {
        // 存储药材id和基地id
        localStorage.setItem("tMedicinalId", row.tMedicinalId);
        localStorage.setItem("tProductBaseId", row.tProductBaseId);
        this.$router.push({
            name: 'medicinalgone'
        })
    },


    // 弹出药品质量
    openmedicineValue(row) {
        this.mdValueform.tMedicinalSharp = row.tMedicinalSharp; // 性状
        this.mdValueform.tMedicinalSharpImgPath = row.tMedicinalSharpImgPath; // 性状图片
        this.mdValueform.tMedicinalIdentify = row.tMedicinalIdentify; // 鉴别
        this.mdValueform.tMedicinalIdentifyImgPath = row.tMedicinalIdentifyImgPath; // 鉴别图片
        this.mdValueform.tMedicinalExtractum = row.tMedicinalExtractum; // 浸出物
        this.mdValueform.tMedicinalContentsCheck = row.tMedicinalContentsCheck; // 含量测定
        this.mdValueform.tMedicinalFeatureImgPath = row.tMedicinalFeatureImgPath; // 特征图谱或指纹图谱
        this.mdValueform.tMedicinalFeatureImgInfo = row.tMedicinalFeatureImgInfo; // 图谱说明
        this.mdValueform.tMedicinalQuality = row.tMedicinalQuality;// 总评
        // 鉴定图片
        console.log(this.mdValueform.tMedicinalIdentifyImgPath)
        this.dialogmedicineValue = true;
    },
};

function created() {
    console.log('当前路径：', this.$route.name)
    this.fetchData();
    this.fetchProductBase();
};


export default {
    BASEURL,
    components,
    data,
    watch,
    methods,
    created
}